<?php
class ControllerCommunityUcp extends Controller{
    private $error=array();

    public function index(){
        if (!$this->customer->isLogged()) {
            $this->redirect($this->url->link('account/login', '', 'SSL'));
        }

        $this->language->load('community/ucp');
        $this->language->load('community/community_header');
        $this->load->model('community/ucp');

        $this->document->setTitle($this->language->get('heading_title'));
        $this->data['heading_title']=$this->language->get('heading_title');

        $role_rank_title=$this->model_community_ucp->getUserRoleTitleAndRankTitle($this->customer->getForumUserType(), $this->customer->getForumUserRank());

        $this->data['userInfo']=array(
            'user_role'             =>      $this->customer->getForumUserType(),
            'role_title'            =>      isset($role_rank_title['role_title']) ? $role_rank_title['role_title']  : '',
            'user_ip'               =>      $this->customer->getForumUserIp(),
            'user_regdate'          =>      date($this->language->get('date_time_format_long'),$this->customer->getForumRegDate()),
            'username'              =>      $this->customer->getForumUserName(),
            'user_birthday'         =>      $this->customer->getForumUserBirthDay(),
            'user_lastvisit'        =>      date($this->language->get('date_time_format_long'),$this->customer->getForumUserLastVisitTime()),
            'user_lastpost_time'    =>      date($this->language->get('date_time_format_long'),$this->customer->getForumUserLastPostTime()),
            'user_warnings'         =>      $this->customer->getForumUserWarnings(),
            'user_ban_id'           =>      $this->customer->getForumUserBanId(),
            'user_topics'           =>      $this->customer->getForumUserTopics(),
            'user_posts'            =>      $this->customer->getForumUserPosts(),
            'rank_title'            =>      isset($role_rank_title['rank_title']) ? $role_rank_title['rank_title'] : '',
            'user_new_privmsg'      =>      $this->customer->getForumUserNewPm(),
            'user_attach_notify'    =>      $this->customer->getForumUserAttachNotify(),
            'user_attach_bbcode'    =>      $this->customer->getForumUserAttachBbcode(),
            'user_attach_smiles'    =>      $this->customer->getForumUserAttachSmiles(),
            'user_attach_sig'       =>      $this->customer->getForumUserAttachSignature(),
            'user_notify_pm'        =>      $this->customer->getForumUserNotifyPm(),
            'user_allow_pm'         =>      $this->customer->getForumUserAllowPm(),
            'user_allow_email'      =>      $this->customer->getForumUserAllowEmail(),
            'user_allow_viewonline' =>      $this->customer->getForumUserAllowViewOnline(),
            'user_avatar'           =>      $this->customer->getForumUserAvatar(),
            'user_sig'              =>      $this->customer->getForumUserSignature(),
            'user_from'             =>      $this->customer->getForumUserLocation(),
            'user_icq'              =>      $this->customer->getForumUserIcq(),
            'user_aim'              =>      $this->customer->getForumUserAim(),
            'user_yim'              =>      $this->customer->getForumUserYim(),
            'user_msnm'             =>      $this->customer->getForumUserMsnm(),
            'user_jabber'           =>      $this->customer->getForumUserJabber(),
            'user_occ'              =>      $this->customer->getForumUserOccupation(),
            'user_interests'        =>      $this->customer->getForumUserInterests(),
            'user_actkey'           =>      $this->customer->getForumUserActkey()
        );

        if($this->customer->getForumUserLastPostTime()=='0'){
            $this->data['userInfo']['user_lastpost_time']=$this->language->get('text_never');
        }

        $this->data['breadcrumbs']=array();
        $this->data['breadcrumbs'][]=array(
            'href'      =>      $this->url->link('common/home'),
            'text'      =>      $this->language->get('text_home'),
            'separator'=>      FALSE
        );

        $this->data['breadcrumbs'][]=array(
            'href'      =>      $this->url->link('community/community'),
            'text'      =>      $this->language->get('text_community'),
            'separator' =>      $this->language->get('text_separator')
        );

        $this->data['breadcrumbs'][]=array(
            'href'      =>      $this->url->link('community/ucp'),
            'text'      =>      $this->language->get('text_ucp'),
            'separator' =>      $this->language->get('text_separator')
        );

        $this->data['headline_link']=$this->url->link('community/ucp','','SSL');
        $this->data['navi_o_link']=$this->url->link('community/ucp','mode=o','SSL');
        $this->data['navi_p_link']=$this->url->link('community/ucp','mode=p','SSL');
        $this->data['navi_bp_link']=$this->url->link('community/ucp','mode=bp','SSL');
        $this->data['navi_pm_link']=$this->url->link('community/ucp','mode=pm','SSL');
        $this->data['navi_u_link']=$this->url->link('community/ucp','mode=u','SSL');
        $this->data['navi_ff_link']=$this->url->link('community/ucp','mode=ff','SSL');

        $this->data['text_ucp']=$this->language->get('text_ucp');
        $this->data['text_separator']=$this->language->get('text_separator');
        $this->data['text_by']=$this->language->get('text_by');
        $this->data['text_mark_all']=$this->language->get('text_mark_all');
        $this->data['text_unmark_all']=$this->language->get('text_unmark_all');
        $this->data['text_delete']=$this->language->get('text_delete');
        $this->data['text_confirm_delete']=$this->language->get('text_confirm_delete');
        $this->data['text_overview']=$this->language->get('text_overview');
        $this->data['text_profile']=$this->language->get('text_profile');
        $this->data['text_board_preferences']=$this->language->get('text_board_preferences');
        $this->data['text_private_messages']=$this->language->get('text_private_messages');
        $this->data['text_usergroups']=$this->language->get('text_usergroups');
        $this->data['text_friends_foes']=$this->language->get('text_friends_foes');

        $this->data['buttons']=array();
        $this->data['button_links']=array();
        $this->data['content_type']='';

        $this->data['navi_o_active']='';
        $this->data['navi_p_active']='';
        $this->data['navi_bp_active']='';
        $this->data['navi_pm_active']='';
        $this->data['navi_u_active']='';
        $this->data['navi_ff_active']='';
        $this->data['text_action']='';

        if(!isset($this->request->get['mode'])){
            $this->request->get['mode']='o';
        }

        $this->data['mode_type']=$this->request->get['mode'];

        switch($this->request->get['mode']){
            case 'o':
                $this->overview();
                break;
            case 'p':
                $this->profile();
                break;
            case 'bp':
                $this->boardPreference();
                break;
            case 'pm':
                $this->privateMessage();
                break;
            case 'ff':
                $this->friendsAndFoes();
                break;
            default:
                $this->redirect($this->url->link('error/not_found'),'','SSL');
        }

        if(file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/community/ucp.tpl')){
            $this->template=$this->config->get('config_template') . '/template/community/ucp.tpl';
        }else{
            $this->template='default/template/community/ucp.tpl';
        }

        $this->children = array(
            'common/column_left',
            'common/column_right',
            'common/content_top',
            'common/content_bottom',
            'common/footer',
            'common/header',
            'community/community_header'
        );

        $this->response->setOutput($this->render());
    }

    private function overview(){
        $this->data['navi_o_active']='class="navi_active"';

        $this->data['buttons']['fp']=$this->language->get('button_front_page');
        $this->data['button_links'][]=$this->url->link('community/ucp','mode=o&action=fp','SSL');

        $this->data['buttons']['ms']=$this->language->get('button_manage_subscriptions');
        $this->data['button_links'][]=$this->url->link('community/ucp','mode=o&action=ms','SSL');

        $this->data['buttons']['mb']=$this->language->get('button_manage_bookmarks');
        $this->data['button_links'][]=$this->url->link('community/ucp','mode=o&action=mb','SSL');

        $this->data['buttons']['md']=$this->language->get('button_manage_drafts');
        $this->data['button_links'][]=$this->url->link('community/ucp','mode=o&action=md','SSL');

        if(!isset($this->request->get['action'])){
            $this->request->get['action']='fp';
        }

        switch($this->request->get['action']){
            case 'fp':
                $this->data['ucp_heading']=$this->language->get('text_front_page');
                $this->data['content_type']='o_fp';
                $this->data['text_action']='fp';

                $this->data['text_welcome']=$this->language->get('text_welcome');
                $this->data['text_join']=$this->language->get('text_join');
                $this->data['text_last_visit']=$this->language->get('text_last_visit');
                $this->data['text_total_topics']=$this->language->get('text_total_topics');
                $this->data['text_total_posts']=$this->language->get('text_total_posts');
                $this->data['text_last_posting_time']=$this->language->get('text_last_posting_time');
                $this->data['text_warning_count']=$this->language->get('text_warning_count');
                $this->data['text_rank']=$this->language->get('text_rank');
                $this->data['text_role']=$this->language->get('text_role');

                break;
            case 'ms':
                if(isset($this->request->post['delete_subscriptions']) && (isset($this->request->post['marked_f_id']) || isset($this->request->post['marked_t_id']))){
                    $topicIdStr=implode(', ',$this->request->post['marked_t_id']);
                    $forumIdStr=implode(', ',$this->request->post['marked_f_id']);

                    $this->model_community_ucp->deleteSubscriptions($topicIdStr,$forumIdStr);

                    $this->session->data['forwarded_link']=$this->url->link('community/ucp','mode=o&action=ms','SSL');
                    $this->session->data['success_type']='unsubscription';

                    $this->redirect($this->url->link('community/success','','SSL'));
                }

                $this->data['ucp_heading']=$this->language->get('text_manage_subscriptions');
                $this->data['content_type']='o_ms';
                $this->data['text_action']='ms';

                $this->data['text_subscription_intro']=$this->language->get('text_subscription_intro');
                $this->data['text_topic_subscription']=$this->language->get('text_topic_subscription');
                $this->data['text_forum_subscription']=$this->language->get('text_forum_subscription');
                $this->data['text_last_post']=$this->language->get('text_last_post');
                $this->data['text_mark']=$this->language->get('text_mark');
                $this->data['text_no_subscriptions']=$this->language->get('text_no_subscriptions');

                $this->data['action_link']=$this->url->link('community/ucp','mode=o&action=ms','SSL');

                //forum subscriptions
                $this->data['forum_subscriptions']=array();
                $results=$this->model_community_ucp->getForumSubscription();

                foreach($results as $result){
                    $this->data['forum_subscriptions'][]=array(
                        'forum_id'               =>      $result['forum_id'],
                        'forum_link'             =>      $this->url->link('community/viewforum','fid=' . $result['forum_id'],'SSL'),
                        'forum_name'             =>      $result['forum_name'],
                        'forum_desc'             =>      $result['forum_desc'],
                        'forum_last_poster_name' =>      $result['forum_last_poster_name'],
                        'forum_last_post_time'   =>      date($this->language->get('date_time_format_long'),$result['forum_last_post_time'])
                    );

                    //get forum read/unread track record
                    if(!empty($results)){
                        if($result['forum_posts']>0){
                            if(isset($result['mark_time'])){
                                $this->data['forumStatus'][]=($result['mark_time']>$result['forum_last_post_time']) ? 'forum_read' : 'forum_unread';
                                $this->data['text_forum_status'][]=($result['mark_time']>$result['forum_last_post_time']) ? $this->language->get('text_forum_read') : $this->language->get('text_forum_unread');
                            }else{
                                if(!$this->customer->isLogged()){
                                    $this->data['forumStatus'][]='forum_read';
                                    $this->data['text_forum_status'][]=$this->language->get('text_forum_read');
                                }else{
                                    $this->data['forumStatus'][]='forum_unread';
                                    $this->data['text_forum_status'][]=$this->language->get('text_forum_unread');
                                }
                            }
                        }else{
                            $this->data['forumStatus'][]='forum_read';
                            $this->data['text_forum_status'][]=$this->language->get('text_forum_read');
                        }
                    }
                }

                //pagination && topic subscriptions
                $pagination = new Pagination();

                $this->data['subscriptions_total']=$pagination->total=$this->model_community_ucp->getTopicSubscriptionTotal();

                if (isset($this->request->get['page'])) {
                    $page = $this->request->get['page'];
                } else {
                    $page = 1;
                }

                $pagination->page = $page;
                $pagination->limit = 20;
                $start=($page-1)*$pagination->limit;
                $pagination->url = $this->url->link('community/ucp', 'mode=o&action=ms&page={page}','SSL');

                $this->data['pagination'] = $pagination->render();

                $this->data['topic_subscriptions']=array();
                $results=$this->model_community_ucp->getTopicSubscription($start,$pagination->limit);

                foreach($results as $result){
                    $this->data['topic_subscriptions'][]=array(
                        'topic_id'               =>      $result['topic_id'],
                        'topic_link'             =>      $this->url->link('community/viewtopic','fid=' . $result['forum_id'] . '&tid=' . $result['topic_id'],'SSL'),
                        'topic_title'            =>      $result['topic_title'],
                        'topic_time'             =>      date($this->language->get('date_time_format_long'),$result['topic_time']),
                        'topic_first_poster_name'=>      $result['topic_first_poster_name'],
                        'topic_last_poster_name' =>      $result['topic_last_poster_name'],
                        'topic_last_post_time'   =>      date($this->language->get('date_time_format_long'),$result['topic_last_post_time'])
                    );

                    //check topic read/unread status
                    if(!empty($results)){
                        if(time()-$result['topic_last_post_time']>(int)$this->config->get('topics_be_read_time')){
                            switch($result['topic_type']){
                                case '0':
                                    $this->data['topicStatus'][]= $result['topic_status'] ? 'topic_locked_read' : 'topic_read';
                                    break;
                                case '1':
                                    $this->data['topicStatus'][]= $result['topic_status']? 'topic_locked_sticky_read' : 'topic_sticky_read';
                                    break;
                                default:
                                    $this->data['topicStatus'][]= $result['topic_status']? 'announcement_topic_locked_read' : 'announcement_topic_read';
                            }

                            $this->data['text_topic_status'][]=$this->language->get('text_topic_read');
                        }else{
                            if(isset($result['mark_time'])){
                                switch($result['topic_type']){
                                    case '0':
                                        if($result['topic_status']){
                                            $this->data['topicStatus'][]=($result['mark_time']>$result['topic_last_post_time']) ? 'topic_locked_read' : 'topic_locked_unread';
                                        }else{
                                            $this->data['topicStatus'][]=($result['mark_time']>$result['topic_last_post_time']) ? 'topic_read' : 'topic_unread';
                                        }
                                        break;
                                    case '1':
                                        if($result['topic_status']){
                                            $this->data['topicStatus'][]=($result['mark_time']>$result['topic_last_post_time']) ? 'topic_locked_sticky_read' : 'topic_locked_sticky_unread';
                                        }else{
                                            $this->data['topicStatus'][]=($result['mark_time']>$result['topic_last_post_time']) ? 'topic_sticky_read' : 'topic_sticky_unread';
                                        }
                                        break;
                                    default:
                                        if($result['topic_status']){
                                            $this->data['topicStatus'][]=($result['mark_time']>$result['topic_last_post_time']) ? 'announcement_topic_locked_read' : 'announcement_topic_locked_unread';
                                        }else{
                                            $this->data['topicStatus'][]=($result['mark_time']>$result['topic_last_post_time']) ? 'announcement_topic_read' : 'announcement_topic_unread';
                                        }
                                }

                                $this->data['text_topic_status'][]=($result['mark_time']>$result['topic_last_post_time']) ? $this->language->get('text_topic_read') : $this->language->get('text_topic_unread');
                            }else{
                                switch($result['topic_type']){
                                    case '0':
                                        $this->data['topicStatus'][]= $result['topic_status'] ? 'topic_locked_unread' : 'topic_unread';
                                        break;
                                    case '1':
                                        $this->data['topicStatus'][]= $result['topic_status'] ? 'topic_locked_sticky_read' : 'topic_sticky_unread';
                                        break;
                                    default:
                                        $this->data['topicStatus'][]= $result['topic_status'] ? 'announcement_topic_locked_unread' : 'announcement_topic_unread';
                                }
                                $this->data['text_topic_status'][]=$this->language->get('text_topic_unread');
                            }
                        }
                    }
                }
                break;
            case 'mb':
                if(isset($this->request->post['delete_bookmarks']) && isset($this->request->post['marked_bookmark_id'])){
                    $idStr=implode(', ',$this->request->post['marked_bookmark_id']);

                    $this->model_community_ucp->deleteBookmarks($idStr);

                    $this->session->data['forwarded_link']=$this->url->link('community/ucp','mode=o&action=mb','SSL');
                    $this->session->data['success_type']='remove_topic_bookmark';

                    $this->redirect($this->url->link('community/success','','SSL'));
                }

                $this->data['ucp_heading']=$this->language->get('text_manage_bookmarks');
                $this->data['content_type']='o_mb';
                $this->data['text_action']='mb';

                $this->data['text_bookmark_intro']=$this->language->get('text_bookmark_intro');
                $this->data['text_bookmarks']=$this->language->get('text_bookmarks');
                $this->data['text_last_post']=$this->language->get('text_last_post');
                $this->data['text_mark']=$this->language->get('text_mark');
                $this->data['text_no_bookmarks']=$this->language->get('text_no_bookmarks');

                $this->data['action_link']=$this->url->link('community/ucp','mode=o&action=mb','SSL');

                //pagination
                $pagination = new Pagination();

                $this->data['bookmarks_total']=$pagination->total=$this->model_community_ucp->getBookmarksTotal();

                if (isset($this->request->get['page'])) {
                    $page = $this->request->get['page'];
                } else {
                    $page = 1;
                }

                $pagination->page = $page;
                $pagination->limit = 20;
                $start=($page-1)*$pagination->limit;
                $pagination->url = $this->url->link('community/ucp', 'mode=o&action=mb&page={page}','SSL');

                $this->data['pagination'] = $pagination->render();

                $this->data['bookmarks']=array();
                $results=$this->model_community_ucp->getBookmarks($start,$pagination->limit);

                foreach($results as $result){
                    $this->data['bookmarks'][]=array(
                        'topic_id'               =>      $result['topic_id'],
                        'topic_link'             =>      $this->url->link('community/viewtopic','fid=' . $result['forum_id'] . '&tid=' . $result['topic_id'],'SSL'),
                        'topic_title'            =>      $result['topic_title'],
                        'topic_time'             =>      date($this->language->get('date_time_format_long'),$result['topic_time']),
                        'topic_first_poster_name'=>      $result['topic_first_poster_name'],
                        'topic_last_poster_name' =>      $result['topic_last_poster_name'],
                        'topic_last_post_time'   =>      date($this->language->get('date_time_format_long'),$result['topic_last_post_time'])
                    );

                    //check topic read/unread status
                    if(!empty($results)){
                        if(time()-$result['topic_last_post_time']>(int)$this->config->get('topics_be_read_time')){
                            switch($result['topic_type']){
                                case '0':
                                    $this->data['topicStatus'][]= $result['topic_status'] ? 'topic_locked_read' : 'topic_read';
                                    break;
                                case '1':
                                    $this->data['topicStatus'][]= $result['topic_status']? 'topic_locked_sticky_read' : 'topic_sticky_read';
                                    break;
                                default:
                                    $this->data['topicStatus'][]= $result['topic_status']? 'announcement_topic_locked_read' : 'announcement_topic_read';
                            }

                            $this->data['text_topic_status'][]=$this->language->get('text_topic_read');
                        }else{
                            if(isset($result['mark_time'])){
                                switch($result['topic_type']){
                                    case '0':
                                        if($result['topic_status']){
                                            $this->data['topicStatus'][]=($result['mark_time']>$result['topic_last_post_time']) ? 'topic_locked_read' : 'topic_locked_unread';
                                        }else{
                                            $this->data['topicStatus'][]=($result['mark_time']>$result['topic_last_post_time']) ? 'topic_read' : 'topic_unread';
                                        }
                                        break;
                                    case '1':
                                        if($result['topic_status']){
                                            $this->data['topicStatus'][]=($result['mark_time']>$result['topic_last_post_time']) ? 'topic_locked_sticky_read' : 'topic_locked_sticky_unread';
                                        }else{
                                            $this->data['topicStatus'][]=($result['mark_time']>$result['topic_last_post_time']) ? 'topic_sticky_read' : 'topic_sticky_unread';
                                        }
                                        break;
                                    default:
                                        if($result['topic_status']){
                                            $this->data['topicStatus'][]=($result['mark_time']>$result['topic_last_post_time']) ? 'announcement_topic_locked_read' : 'announcement_topic_locked_unread';
                                        }else{
                                            $this->data['topicStatus'][]=($result['mark_time']>$result['topic_last_post_time']) ? 'announcement_topic_read' : 'announcement_topic_unread';
                                        }
                                }

                                $this->data['text_topic_status'][]=($result['mark_time']>$result['topic_last_post_time']) ? $this->language->get('text_topic_read') : $this->language->get('text_topic_unread');
                            }else{
                                switch($result['topic_type']){
                                    case '0':
                                        $this->data['topicStatus'][]= $result['topic_status'] ? 'topic_locked_unread' : 'topic_unread';
                                        break;
                                    case '1':
                                        $this->data['topicStatus'][]= $result['topic_status'] ? 'topic_locked_sticky_read' : 'topic_sticky_unread';
                                        break;
                                    default:
                                        $this->data['topicStatus'][]= $result['topic_status'] ? 'announcement_topic_locked_unread' : 'announcement_topic_unread';
                                }
                                $this->data['text_topic_status'][]=$this->language->get('text_topic_unread');
                            }
                        }
                    }
                }
                break;
            case 'md':

                if(isset($this->request->post['delete_drafts']) && isset($this->request->post['marked_draft_id'])){
                    $idStr=implode(', ',$this->request->post['marked_draft_id']);

                    $this->model_community_ucp->deleteDrafts($idStr);

                    $this->session->data['forwarded_link']=$this->url->link('community/ucp','mode=o&action=md','SSL');
                    $this->session->data['success_type']='draft_del';

                    $this->redirect($this->url->link('community/success','','SSL'));
                }

                $this->data['ucp_heading']=$this->language->get('text_manage_drafts');
                $this->data['content_type']='o_md';
                $this->data['text_action']='md';

                $this->data['text_draft_intro']=$this->language->get('text_draft_intro');
                $this->data['text_draft_title']=$this->language->get('text_draft_title');
                $this->data['text_draft_save_at']=$this->language->get('text_draft_save_at');
                $this->data['text_draft_mark']=$this->language->get('text_draft_mark');

                $this->data['action_link']=$this->url->link('community/ucp','mode=o&action=md','SSL');

                $results=$this->model_community_ucp->getDrafts();

                $this->data['draft_total']=count($results);

                if(empty($results)){
                    $this->data['text_no_drafts']=$this->language->get('text_no_drafts');
                }else{
                    $this->data['drafts']=array();

                    $i=0;

                    foreach($results as $result){
                        $this->data['drafts'][]=array(
                            'draft_id'      =>      $result['draft_id'],
                            'save_time'     =>      date($this->language->get('date_time_format_long'),strtotime($result['save_time'])),
                            'draft_subject' =>      $result['draft_subject'],
                            'draft_contents'=>      $result['draft_contents'],
                        );

                        if(isset($result['topic_title'])){
                            $this->data['drafts'][$i]['text_draft_location']=$this->language->get('text_topic');
                            $this->data['drafts'][$i]['draft_location']=$result['topic_title'];
                            $this->data['drafts'][$i]['draft_location_link']=$this->url->link('community/viewtopic','fid=' . $result['topic_forum_id'] . '&tid=' . $result['topic_id'],'SSL');
                            $this->data['drafts'][$i]['draft_load_link']=$this->url->link('community/post/reply','fid=' . $result['topic_forum_id'] . '&tid=' . $result['topic_id'] . '&did=' . $result['draft_id'],'SSL');
                        }else{
                            $this->data['drafts'][$i]['text_draft_location']=$this->language->get('text_forum');
                            $this->data['drafts'][$i]['draft_location']=$result['forum_name'];
                            $this->data['drafts'][$i]['draft_location_link']=$this->url->link('community/viewforum','fid=' . $result['forum_id'],'SSL');
                            $this->data['drafts'][$i]['draft_load_link']=$this->url->link('community/post/topic','fid=' . $result['forum_id'] . '&did=' . $result['draft_id'],'SSL');
                        }

                        $i++;
                    }
                }
                break;
        }
    }

    private function profile(){
        $this->data['navi_p_active']='class="navi_active"';

        $this->data['buttons']['ep']=$this->language->get('button_edit_profile');
        $this->data['button_links'][]=$this->url->link('community/ucp','mode=p&action=ep','SSL');

        $this->data['buttons']['es']=$this->language->get('button_edit_signature');
        $this->data['button_links'][]=$this->url->link('community/ucp','mode=p&action=es','SSL');

        $this->data['buttons']['ea']=$this->language->get('button_edit_avatar');
        $this->data['button_links'][]=$this->url->link('community/ucp','mode=p&action=ea','SSL');

        $this->data['buttons']['eas']=$this->language->get('button_edit_account_settings');
        $this->data['button_links'][]=$this->url->link('community/ucp','mode=p&action=eas','SSL');

        if(!isset($this->request->get['action'])){
            $this->request->get['action']='ep';
        }

        switch($this->request->get['action']){
            case 'ep':

                if(isset($this->request->post['submit']) && $this->profileEditValidate()){
                    $this->model_community_ucp->updateProfile($this->request->post);

                    $this->session->data['forwarded_link']=$this->url->link('community/ucp','mode=p&action=ep','SSL');
                    $this->session->data['success_type']='profile_update';

                    $this->redirect($this->url->link('community/success','','SSL'));
                }

                $this->data['ucp_heading']=$this->language->get('text_edit_profile');
                $this->data['content_type']='p_ep';
                $this->data['text_action']='ep';

                $this->data['action_link']=$this->url->link('community/ucp','mode=p&action=ep','SSL');

                $this->data['text_edit_profile_intro']=$this->language->get('text_edit_profile_intro');
                $this->data['text_submit']=$this->language->get('text_submit');

                //entry
                $this->data['entry_icq']=$this->language->get('entry_icq');
                $this->data['entry_aol']=$this->language->get('entry_aol');
                $this->data['entry_wl_msn']=$this->language->get('entry_wl_msn');
                $this->data['entry_yahoo']=$this->language->get('entry_yahoo');
                $this->data['entry_jabber']=$this->language->get('entry_jabber');
                $this->data['entry_location']=$this->language->get('entry_location');
                $this->data['entry_occupation']=$this->language->get('entry_occupation');
                $this->data['entry_interests']=$this->language->get('entry_interests');
                $this->data['entry_birthday']=$this->language->get('entry_birthday');
                $this->data['entry_birthday_day']=$this->language->get('entry_birthday_day');
                $this->data['entry_birthday_month']=$this->language->get('entry_birthday_month');
                $this->data['entry_birthday_year']=$this->language->get('entry_birthday_year');

                //produce digits for birthday list
                $this->data['birthday_day_list']=array();
                $this->data['birthday_day_list'][0]='<option value="--">--</option>';
                foreach(range(1,31) as $day){
                    $this->data['birthday_day_list'][]='<option value="' . $day . '">' . $day . '</option>';
                }

                $this->data['birthday_month_list']=array();
                $this->data['birthday_month_list'][0]='<option value="--">--</option>';
                foreach(range(1,12) as $month){
                    $this->data['birthday_month_list'][]='<option value="' . $month . '">' . $month . '</option>';
                }

                $this->data['birthday_year_list']=array();
                $this->data['birthday_year_list'][0]='<option value="--">--</option>';
                foreach(range(date('Y',time())-100,date('Y',time())) as $year){
                    $this->data['birthday_year_list'][]='<option value="' . $year . '">' . $year . '</option>';
                }

                //error
                if(isset($this->error['icq'])){
                    $this->data['error_icq']=$this->error['icq'];
                }else{
                    $this->data['error_icq']='';
                }

                if(isset($this->error['aol'])){
                    $this->data['error_aol']=$this->error['aol'];
                }else{
                    $this->data['error_aol']='';
                }

                if(isset($this->error['wl_msn'])){
                    $this->data['error_wl_msn']=$this->error['wl_msn'];
                }else{
                    $this->data['error_wl_msn']='';
                }

                if(isset($this->error['yahoo'])){
                    $this->data['error_yahoo']=$this->error['yahoo'];
                }else{
                    $this->data['error_yahoo']='';
                }

                if(isset($this->error['jabber'])){
                    $this->data['error_jabber']=$this->error['jabber'];
                }else{
                    $this->data['error_jabber']='';
                }

                if(isset($this->error['birthday'])){
                    $this->data['error_birthday']=$this->error['birthday'];
                }else{
                    $this->data['error_birthday']='';
                }

                //values stored in $_POST
                if(isset($this->request->post['icq'])){
                    $this->data['icq']=$this->request->post['icq'];
                }else{
                    $this->data['icq']=$this->data['userInfo']['user_icq'];
                }

                if(isset($this->request->post['aol'])){
                    $this->data['aol']=$this->request->post['aol'];
                }else{
                    $this->data['aol']=$this->data['userInfo']['user_aim'];
                }

                if(isset($this->request->post['aol'])){
                    $this->data['wl_msn']=$this->request->post['wl_msn'];
                }else{
                    $this->data['wl_msn']=$this->data['userInfo']['user_msnm'];
                }

                if(isset($this->request->post['yahoo'])){
                    $this->data['yahoo']=$this->request->post['yahoo'];
                }else{
                    $this->data['yahoo']=$this->data['userInfo']['user_yim'];
                }

                if(isset($this->request->post['jabber'])){
                    $this->data['jabber']=$this->request->post['jabber'];
                }else{
                    $this->data['jabber']=$this->data['userInfo']['user_jabber'];
                }

                if(isset($this->request->post['location'])){
                    $this->data['location']=$this->request->post['location'];
                }else{
                    $this->data['location']=$this->data['userInfo']['user_from'];
                }

                if(isset($this->request->post['occupation'])){
                    $this->data['occupation']=$this->request->post['occupation'];
                }else{
                    $this->data['occupation']=$this->data['userInfo']['user_occ'];
                }

                if(isset($this->request->post['interests'])){
                    $this->data['interests']=$this->request->post['interests'];
                }else{
                    $this->data['interests']=$this->data['userInfo']['user_interests'];
                }


                if((isset($this->request->post['birthday_day']) || isset($this->request->post['birthday_month']) || isset($this->request->post['birthday_year'])) && ($this->request->post['birthday_day']!='--' && $this->request->post['birthday_month']!='--' && $this->request->post['birthday_year']!='--')){
                    $this->data['birthday_day_list'][$this->request->post['birthday_day']]='<option value="' . $this->request->post['birthday_day'] . '" selected="selected">' . $this->request->post['birthday_day'] . '</option>';
                    $this->data['birthday_month_list'][$this->request->post['birthday_month']]='<option value="' . $this->request->post['birthday_month'] . '" selected="selected">' . $this->request->post['birthday_month'] . '</option>';
                    $this->data['birthday_year_list'][$this->request->post['birthday_year']-date('Y',time())+101]='<option value="' . $this->request->post['birthday_year'] . '" selected="selected">' . $this->request->post['birthday_year'] . '</option>';
                }else{
                    if(!empty($this->data['userInfo']['user_birthday'])){
                        list($birthday_month,$birthday_day,$birthday_year)=explode('-',$this->data['userInfo']['user_birthday']);

                        $this->data['birthday_day_list'][$birthday_day]='<option value="' . $birthday_day . '" selected="selected">' . $birthday_day . '</option>';
                        $this->data['birthday_month_list'][$birthday_month]='<option value="' . $birthday_month . '" selected="selected">' . $birthday_month . '</option>';
                        $this->data['birthday_year_list'][$birthday_year-date('Y',time())+101]='<option value="' . $birthday_year . '" selected="selected">' . $birthday_year . '</option>';
                    }
                }
                break;
            case 'es':

                if(isset($this->request->post['submit']) && $this->signatureValidate()){
                    $this->model_community_ucp->updateSignature($this->request->post);

                    $this->session->data['forwarded_link']=$this->url->link('community/ucp','mode=p&action=es','SSL');
                    $this->session->data['success_type']='profile_update';

                    $this->redirect($this->url->link('community/success','','SSL'));
                }

                $this->data['ucp_heading']=$this->language->get('text_edit_signature');
                $this->data['content_type']='p_es';
                $this->data['text_action']='es';

                $this->data['action_link']=$this->url->link('community/ucp','mode=p&action=es','SSL');

                $this->data['text_edit_profile_intro']=$this->language->get('text_edit_signature_intro');
                $this->data['text_submit']=$this->language->get('text_submit');

                if($this->config->get('bbcode_parsing_enable')){
                    $bbButtonTag=$this->generateBbcode();

                    $this->data['bb_buttons']=$bbButtonTag[0];
                    $this->data['bb_tags']=$bbButtonTag[1];

                    $this->data['bbcode_enable']=TRUE;
                }else{
                    $this->data['bbcode_enable']=FALSE;
                }

                $this->data['smiles']=$this->generateSmiles();

                //bbcode
                $this->data['text_font_color']=$this->language->get('text_font_color');
                $this->data['text_font_color_close']=$this->language->get('text_font_color_close');

                //ubbcode enable or disable
                $this->data['text_bbcode_enabled']=$this->language->get('text_bbcode_enabled');
                $this->data['text_img_enabled']=$this->language->get('text_img_enabled');
                $this->data['text_flash_enabled']=$this->language->get('text_flash_enabled');
                $this->data['text_url_enabled']=$this->language->get('text_url_enabled');
                $this->data['text_smile_enabled']=$this->language->get('text_smile_enabled');
                $this->data['text_bbcode_disabled']=$this->language->get('text_bbcode_disabled');
                $this->data['text_img_disabled']=$this->language->get('text_img_disabled');
                $this->data['text_flash_disabled']=$this->language->get('text_flash_disabled');
                $this->data['text_url_disabled']=$this->language->get('text_url_disabled');
                $this->data['text_smile_disabled']=$this->language->get('text_smile_disabled');

                //error
                if(isset($this->error['content'])){
                    $this->data['error_content']=$this->error['content'];
                }else{
                    $this->data['error_content']='';
                }

                //values stored in $_POST
                if(isset($this->request->post['content'])){
                    $this->data['content']=$this->request->post['content'];
                }else{
                    $this->data['content']=$this->data['userInfo']['user_sig'];
                }

                break;
            case 'ea':
                $this->data['ucp_heading']=$this->language->get('text_edit_avatar');
                $this->data['content_type']='p_ea';
                $this->data['text_action']='ea';

                if(isset($this->request->post['submit']) && $avatarPath=$this->avatarValidate()){
                    $oldAvatarPath=$this->data['userInfo']['user_avatar'];

                    if(!empty($oldAvatarPath)){
                        unlink($oldAvatarPath);
                    }

                    $this->model_community_ucp->updateAvatar($avatarPath);

                    $this->session->data['forwarded_link']=$this->url->link('community/ucp','mode=p&action=ea','SSL');
                    $this->session->data['success_type']='profile_update';

                    $this->redirect($this->url->link('community/success','','SSL'));
                }

                $this->data['action_link']=$this->url->link('community/ucp','mode=p&action=ea','SSL');

                $this->data['text_edit_avatar_intro']=sprintf($this->language->get('text_edit_avatar_intro'),$this->config->get('max_avatar_width'),$this->config->get('max_avatar_height'),$this->config->get('max_avatar_size'));
                $this->data['text_max_file_size'] = $this->config->get('max_avatar_size')*1024;
                $this->data['text_current_avatar']=$this->language->get('text_current_avatar');
                $this->data['text_submit']=$this->language->get('text_submit');

                //entry
                $this->data['entry_upoad_avatar']=$this->language->get('entry_upoad_avatar');

                //error
                if(isset($this->error['avatar'])){
                    $this->data['error_avatar']=$this->error['avatar'];
                }else{
                    $this->data['error_avatar']='';
                }

                break;
            case 'eas':
                if(isset($this->request->post['submit']) && $this->forumUsernameValidate()){
                    $this->model_community_ucp->updateForumUserName($this->request->post);

                    $this->session->data['forwarded_link']=$this->url->link('community/ucp','mode=p&action=eas','SSL');
                    $this->session->data['success_type']='profile_update';

                    $this->redirect($this->url->link('community/success','','SSL'));
                }

                $this->data['ucp_heading']=$this->language->get('text_edit_account_settings');
                $this->data['content_type']='p_eas';
                $this->data['text_action']='eas';
                $this->data['text_edit_accout_settings_intro']=$this->language->get('text_edit_accout_settings_intro');
                $this->data['text_edit_you_account']=$this->language->get('text_edit_you_account');
                $this->data['text_forum_name']=$this->language->get('text_forum_name');
                $this->data['text_update_forum_name']=$this->language->get('text_update_forum_name');
                $this->data['text_submit']=$this->language->get('text_submit');

                $this->data['go_link']=$this->url->link('account/account','','SSL');
                $this->data['action_link']=$this->url->link('community/ucp','mode=p&action=eas','SSL');

                //error
                if(isset($this->error['forumusername'])){
                    $this->data['error_forumusername']=$this->error['forumusername'];
                }else{
                    $this->data['error_forumusername']='';
                }
                break;
        }
    }

    private function boardPreference(){
        $this->data['navi_bp_active']='class="navi_active"';

        $this->data['buttons']['egs']=$this->language->get('button_edit_global_setting');
        $this->data['button_links'][]=$this->url->link('community/ucp','mode=bp&action=egs','SSL');

        $this->data['buttons']['epd']=$this->language->get('button_edit_posting_default');
        $this->data['button_links'][]=$this->url->link('community/ucp','mode=bp&action=epd','SSL');

        if(!isset($this->request->get['action'])){
            $this->request->get['action']='egs';
        }

        switch($this->request->get['action']){
            case 'egs':

                if(isset($this->request->post['submit'])){
                    $this->model_community_ucp->updateGlobalSettings($this->request->post);

                    $this->session->data['forwarded_link']=$this->url->link('community/ucp','mode=bp&action=egs','SSL');
                    $this->session->data['success_type']='board_preference_update';

                    $this->redirect($this->url->link('community/success','','SSL'));
                }

                $this->data['ucp_heading']=$this->language->get('text_edit_global_setting');
                $this->data['content_type']='bp_egs';
                $this->data['text_action']='egs';

                $this->data['text_edit_global_settings_intro']=$this->language->get('text_edit_global_settings_intro');
                $this->data['text_yes']=$this->language->get('text_yes');
                $this->data['text_no']=$this->language->get('text_no');
                $this->data['text_allow_user_email']=$this->language->get('text_allow_user_email');
                $this->data['text_allow_user_pm']=$this->language->get('text_allow_user_pm');
                $this->data['text_allow_user_pm_subtitle']=$this->language->get('text_allow_user_pm_subtitle');
                $this->data['text_hide_online_status']=$this->language->get('text_hide_online_status');
                $this->data['text_hide_online_status_subtitle']=$this->language->get('text_hide_online_status_subtitle');
                $this->data['text_pm_notify']=$this->language->get('text_pm_notify');
                $this->data['text_submit']=$this->language->get('text_submit');

                $this->data['action_link']=$this->url->link('community/ucp','mode=bp&action=egs','SSL');

                $this->data['text_allow_user_email_yes']=$this->data['userInfo']['user_allow_email'] ? 'checked="checked"' : '';
                $this->data['text_allow_user_email_no']=$this->data['userInfo']['user_allow_email'] ? '' : 'checked="checked"';
                $this->data['text_allow_user_pm_yes']=$this->data['userInfo']['user_allow_pm'] ? 'checked="checked"' : '';
                $this->data['text_allow_user_pm_no']=$this->data['userInfo']['user_allow_pm'] ? '' : 'checked="checked"';
                $this->data['text_hide_online_status_no']=$this->data['userInfo']['user_allow_viewonline'] ? 'checked="checked"' : '';
                $this->data['text_hide_online_status_yes']=$this->data['userInfo']['user_allow_viewonline'] ? '' : 'checked="checked"';
                $this->data['text_pm_notify_yes']=$this->data['userInfo']['user_notify_pm'] ? 'checked="checked"' : '';
                $this->data['text_pm_notify_no']=$this->data['userInfo']['user_notify_pm'] ? '' : 'checked="checked"';

                break;
            case 'epd':

                if(isset($this->request->post['submit'])){
                    $this->model_community_ucp->updatePostingDefault($this->request->post);

                    $this->session->data['forwarded_link']=$this->url->link('community/ucp','mode=bp&action=epd','SSL');
                    $this->session->data['success_type']='board_preference_update';

                    $this->redirect($this->url->link('community/success','','SSL'));
                }

                $this->data['ucp_heading']=$this->language->get('text_edit_posting_default');
                $this->data['content_type']='bp_epd';
                $this->data['text_action']='epd';

                $this->data['text_edit_posting_default_intro']=$this->language->get('text_edit_posting_default_intro');
                $this->data['text_yes']=$this->language->get('text_yes');
                $this->data['text_no']=$this->language->get('text_no');
                $this->data['text_enable_bbcode']=$this->language->get('text_enable_bbcode');
                $this->data['text_enable_smiles']=$this->language->get('text_enable_smiles');
                $this->data['text_attach_signature']=$this->language->get('text_attach_signature');
                $this->data['text_reply_notify']=$this->language->get('text_reply_notify');
                $this->data['text_submit']=$this->language->get('text_submit');

                $this->data['action_link']=$this->url->link('community/ucp','mode=bp&action=epd','SSL');

                $this->data['text_enable_bbcode_yes']=$this->data['userInfo']['user_attach_bbcode'] ? 'checked="checked"' : '';
                $this->data['text_enable_bbcode_no']=$this->data['userInfo']['user_attach_bbcode'] ? '' : 'checked="checked"';
                $this->data['text_enable_smiles_yes']=$this->data['userInfo']['user_attach_smiles'] ? 'checked="checked"' : '';
                $this->data['text_enable_smiles_no']=$this->data['userInfo']['user_attach_smiles'] ? '' : 'checked="checked"';
                $this->data['text_attach_signature_yes']=$this->data['userInfo']['user_attach_sig'] ? 'checked="checked"' : '';
                $this->data['text_attach_signature_no']=$this->data['userInfo']['user_attach_sig'] ? '' : 'checked="checked"';
                $this->data['text_reply_notify_yes']=$this->data['userInfo']['user_attach_notify'] ? 'checked="checked"' : '';
                $this->data['text_reply_notify_no']=$this->data['userInfo']['user_attach_notify'] ? '' : 'checked="checked"';

                break;
        }
    }

    private function privateMessage(){
        if($this->data['userInfo']['user_new_privmsg']==0 || $this->customer->getForumUserNotifyPm()==0){
            $unreadMessageNum='';
        }else{
            $unreadMessageNum=' (' . $this->data['userInfo']['user_new_privmsg'] . ')';
        }

        $this->data['navi_pm_active']='class="navi_active"';

        $this->data['buttons']['cm']=$this->language->get('button_compose_message');
        $this->data['button_links'][]=$this->url->link('community/ucp','mode=pm&action=cm','SSL');

        $this->data['buttons']['ib']=$this->language->get('button_inbox') . $unreadMessageNum;
        $this->data['button_links'][]=$this->url->link('community/ucp','mode=pm&action=ib','SSL');

        $this->data['buttons']['sm']=$this->language->get('button_sent_messages');
        $this->data['button_links'][]=$this->url->link('community/ucp','mode=pm&action=sm','SSL');

        if(!isset($this->request->get['action'])){
            $this->request->get['action']='ib';
        }

        switch($this->request->get['action']){
            //compose messeage
            case 'cm':
                if(isset($this->request->post['add_member_submit'])){
                    if(!empty($this->request->post['add_member'])){
                        $result=$this->model_community_ucp->checkUser($this->request->post);

                        if(isset($result['user_id'])){
                            if($result['user_id']==$this->customer->getForumUserId()){
                                $this->data['error_to_member']=$this->language->get('error_to_member_self');
                            }elseif($result['user_allow_pm']==0){
                                $this->data['error_to_member']=$this->language->get('error_to_member_not_allowed');
                            }else{
                                $this->session->data['to_member_list'][$this->request->post['add_member']]=$this->request->post['add_member'];
                                $this->session->data['to_member_id_list'][$this->request->post['add_member']]=$result['user_id'];
                            }
                        }else{
                            $this->data['error_to_member']=$this->language->get('error_to_member');
                        }
                    }else{
                        $this->data['error_to_member']=$this->language->get('error_to_member_none');
                    }
                }

                $this->data['action_link']=$this->url->link('community/ucp','mode=pm&action=cm','SSL');

                $subject='';

                //reply private message or click the pm button on topic page
                if(isset($this->request->get['uid'])){// reply pm
                    if(isset($this->request->get['mid'])  && isset($this->request->get['hid'])){
                        $this->data['action_link']=$this->url->link('community/ucp','mode=pm&action=cm&mid=' .$this->request->get['mid']  . '&hid=' . $this->request->get['hid'] . '&uid=' . $this->request->get['uid'],'SSL');

                        $result=$this->model_community_ucp->getUserAndPmInfo($this->request->get);

                        $reply_prefix=substr($result['message_subject'],0,3);

                        if(strtolower($reply_prefix)=='re:'){
                            $subject=$result['message_subject'];
                        }else{
                            $subject='Re:' . $result['message_subject'];
                        }

                        if(isset($result['username'])){
                            if($this->request->get['uid']==$this->customer->getForumUserId()){
                                $this->data['error_to_member']=$this->language->get('error_to_member_self');
                            }elseif($result['user_allow_pm']==0){
                                $this->data['error_to_member']=$this->language->get('error_to_member_not_allowed');
                            }else{
                                $this->session->data['to_member_list'][$result['username']]=$result['username'];
                                $this->session->data['to_member_id_list'][$this->request->get['uid']]=$this->request->get['uid'];
                            }
                        }else{
                            $this->data['error_to_member']=$this->language->get('error_to_member');
                        }
                    }else{//pm members from the topic page
                        $this->data['action_link']=$this->url->link('community/ucp','mode=pm&action=cm&uid=' . $this->request->get['uid'],'SSL');

                        $result=$this->model_community_ucp->getUserInfo($this->request->get);

                        if(isset($result['username'])){
                            if($this->request->get['uid']==$this->customer->getForumUserId()){
                                $this->data['error_to_member']=$this->language->get('error_to_member_self');
                            }elseif($result['user_allow_pm']==0){
                                $this->data['error_to_member']=$this->language->get('error_to_member_not_allowed');
                            }else{
                                $this->session->data['to_member_list'][$result['username']]=$result['username'];
                                $this->session->data['to_member_id_list'][$this->request->get['uid']]=$this->request->get['uid'];
                            }
                        }else{
                            $this->data['error_to_member']=$this->language->get('error_to_member');
                        }
                    }
                }

                if(isset($this->session->data['to_member_list'])){
                    $this->data['to_member_list']=$this->session->data['to_member_list'];
                }

                if(isset($this->request->post['remove_to_member'])){
                    unset($this->data['to_member_list'][$this->request->post['remove_to_member']]);
                    unset($this->session->data['to_member_list'][$this->request->post['remove_to_member']]);
                    unset($this->session->data['to_member_id_list'][$this->request->post['add_member']]);
                }

                if(isset($this->request->post['send']) && isset($this->session->data['to_member_list']) && $this->pmValidate()){
                    $this->model_community_ucp->addPrivateMessage($this->session->data['to_member_id_list'],$this->request->post,$this->request->get);

                    unset($this->data['to_member_list']);
                    unset($this->session->data['to_member_list']);
                    unset($this->session->data['to_member_id_list']);

                    $this->session->data['forwarded_link']=$this->url->link('community/ucp','mode=pm&action=sm','SSL');
                    $this->session->data['success_type']='pm_sent';

                    $this->redirect($this->url->link('community/success','','SSL'));
                }

                $this->data['ucp_heading']=$this->language->get('text_compose_message');
                $this->data['content_type']='pm_cm';
                $this->data['text_action']='cm';
                $this->data['text_to']=$this->language->get('text_to');
                $this->data['entry_subject']=$this->language->get('entry_subject');
                $this->data['entry_add_member']=$this->language->get('entry_add_member');

                if($this->config->get('bbcode_parsing_enable')){
                    $bbButtonTag=$this->generateBbcode();

                    $this->data['bb_buttons']=$bbButtonTag[0];
                    $this->data['bb_tags']=$bbButtonTag[1];

                    $this->data['bbcode_enable']=TRUE;
                }else{
                    $this->data['bbcode_enable']=FALSE;
                }

                $this->data['smiles']=$this->generateSmiles();

                //bbcode
                $this->data['text_font_color']=$this->language->get('text_font_color');
                $this->data['text_font_color_close']=$this->language->get('text_font_color_close');
                //ubbcode enable or disable
                $this->data['text_bbcode_enabled']=$this->language->get('text_bbcode_enabled');
                $this->data['text_img_enabled']=$this->language->get('text_img_enabled');
                $this->data['text_flash_enabled']=$this->language->get('text_flash_enabled');
                $this->data['text_url_enabled']=$this->language->get('text_url_enabled');
                $this->data['text_smile_enabled']=$this->language->get('text_smile_enabled');
                $this->data['text_bbcode_disabled']=$this->language->get('text_bbcode_disabled');
                $this->data['text_img_disabled']=$this->language->get('text_img_disabled');
                $this->data['text_flash_disabled']=$this->language->get('text_flash_disabled');
                $this->data['text_url_disabled']=$this->language->get('text_url_disabled');
                $this->data['text_smile_disabled']=$this->language->get('text_smile_disabled');
                //button
                $this->data['text_send']=$this->language->get('text_send');
                $this->data['text_add']=$this->language->get('text_add');

                //error
                if(isset($this->error['subject'])){
                    $this->data['error_subject']=$this->error['subject'];
                }else{
                    $this->data['error_subject']='';
                }

                if(isset($this->error['content'])){
                    $this->data['error_content']=$this->error['content'];
                }else{
                    $this->data['error_content']='';
                }

                //values stored in $_POST
                if(isset($this->request->post['subject'])){
                    $this->data['subject']=$this->request->post['subject'];
                }else{
                    $this->data['subject']=$subject;
                }

                if(isset($this->request->post['content'])){
                    $this->data['content']=$this->request->post['content'];
                }else{
                    $this->data['content']='';
                }

                break;
            //inbox
            case 'ib':
                if(isset($this->request->post['delete_pm']) && isset($this->request->post['marked_msg_id'])){
                    $idStr=implode(', ',$this->request->post['marked_msg_id']);

                    $this->model_community_ucp->deletePrivateMessages($idStr,$this->request->post['marked_msg_id']);

                    $this->session->data['forwarded_link']=$this->url->link('community/ucp','mode=pm&action=ib','SSL');
                    $this->session->data['success_type']='pm_del';

                    $this->redirect($this->url->link('community/success','','SSL'));
                }

                $this->data['ucp_heading']=$this->language->get('text_inbox');
                $this->data['content_type']='pm_ib';
                $this->data['text_action']='ib';

                $this->data['text_from']=$this->language->get('text_from');
                $this->data['text_sent']=$this->language->get('text_sent');
                $this->data['text_no_messages']=$this->language->get('text_no_messages');
                $this->data['text_message']=$this->language->get('text_message');
                $this->data['text_mark']=$this->language->get('text_mark');
                $this->data['text_message_history']=$this->language->get('text_message_history');
                //button
                $this->data['text_delete_pm']=$this->language->get('text_delete_pm');
                $this->data['text_report_pm']=$this->language->get('text_report_pm');

                //private message in detail view
                if(isset($this->request->get['mid'])){
                    $this->data['has_mid']='';

                    if(isset($this->request->post['delete_pm'])){
                        $idArr[]=$this->request->get['mid'];

                        $this->model_community_ucp->deletePrivateMessages($this->request->get['mid'],$idArr);

                        $this->session->data['forwarded_link']=$this->url->link('community/ucp','mode=pm&action=ib','SSL');
                        $this->session->data['success_type']='pm_del';

                        $this->redirect($this->url->link('community/success','','SSL'));
                    }

                    //detect if folder is full
                    $messageTotal=$this->model_community_ucp->getPrivateMessageTotal();

                    if($messageTotal>$this->config->get('inbox_max_pm')){
                        $validMessageTotal=$this->config->get('inbox_max_pm');
                    }else{
                        $validMessageTotal=$messageTotal;
                    }

                    $inboxTotal=$validMessageTotal;

                    $this->data['text_inbox_folder_full']=sprintf($this->language->get('text_inbox_folder_full'),(($inboxTotal/$this->config->get('inbox_max_pm'))*100) . '%',$inboxTotal,$this->config->get('inbox_max_pm'));

                    if($inboxTotal==$this->config->get('inbox_max_pm')){
                        $this->data['is_folder_full']=TRUE;
                        $this->data['error_folder_full']=$this->language->get('error_folder_full');
                    }else{
                        $this->data['is_folder_full']=FALSE;
                    }

                    $result=$this->model_community_ucp->viewPrivateMessage($this->request->get);

                    $this->data['private_message']=array(
                        'user_profile_url'  =>  $this->url->link('community/profile','uid=' . $result['author_id'] ,'SSL'),
                        'username'          =>  $result['username'],
                        'msg_history_id'    =>  $result['msg_history_id'],
                        'message_time'      =>  date($this->language->get('date_time_format_long'),$result['message_time']),
                        'message_subject'   =>  $result['message_subject'],
                        'message_text'      =>  $this->parseSmiles($this->parseBbcode($this->parseSpacing($result['message_text']))),
                        'delete_link'       =>  $this->url->link('community/',$result['msg_id'],'SSL'),
                        'report_link'       =>  $this->url->link('community/report','type=ib' . '&mid=' . $result['msg_id'],'SSL')
                    );

                    if($result['pm_unread']){
                        $this->model_community_ucp->updateMessageReadStatus($this->request->get);
                    }

                    $this->data['action_link']=$this->url->link('community/ucp','mode=pm&action=ib&mid=' . $result['msg_id'],'SSL');
                    $this->data['reply_pm_link']=$this->url->link('community/ucp','mode=pm&action=cm&mid=' .  $result['msg_id'] . '&hid=' . $result['msg_history_id'] . '&uid=' . $result['author_id'],'SSL');

                    if(!$result['message_reported']){
                        $this->data['show_report_icon']=TRUE;
                    }else{
                        $this->data['show_report_icon']=FALSE;
                    }

                    //private message history
                    $results=$this->model_community_ucp->viewPrivateMessageHistory($this->request->get,$result['msg_history_id']);

                    if(count($results)>1){
                        $this->data['has_message_history']=TRUE;
                    }else{
                        $this->data['has_message_history']=FALSE;
                    }


                    if($this->data['has_message_history']){
                        foreach($results as $result){
                            $this->data['private_message_histories'][]=array(
                                'user_profile_url'  =>  $this->url->link('community/profile','uid=' . $result['author_id'] ,'SSL'),
                                'username'          =>  $result['username'],
                                'message_time'      =>  date($this->language->get('date_time_format_long'),$result['message_time']),
                                'message_subject'   =>  $result['message_subject'],
                                'message_text'      =>  $this->parseSmiles($this->parseBbcode($this->parseSpacing($result['message_text'])))
                            );
                        }
                    }
                }else{//private message in list view
                    $this->data['action_link']=$this->url->link('community/ucp','mode=pm&action=ib','SSL');
                    $this->data['newpm_link']=$this->url->link('community/ucp','mode=pm&action=cm','SSL');

                    //pagination
                    $pagination = new Pagination();

                    if (isset($this->request->get['page'])) {
                        $page = $this->request->get['page'];
                    } else {
                        $page = 1;
                    }

                    $pagination->page = $page;
                    $pagination->limit = 20;
                    $pagination->url = $this->url->link('community/ucp', '&mode=pm&action=ib&page={page}','SSL');

                    $this->data['pagination'] = $pagination->render();

                    //detect if folder is full
                    $messageTotal=$this->model_community_ucp->getPrivateMessageTotal();

                    if($messageTotal>$this->config->get('inbox_max_pm')){
                        $validMessageTotal=$this->config->get('inbox_max_pm');
                    }else{
                        $validMessageTotal=$messageTotal;
                    }

                    $this->data['inbox_total']=$pagination->total=$validMessageTotal;

                    $this->data['text_inbox_folder_full']=sprintf($this->language->get('text_inbox_folder_full'),(($pagination->total/$this->config->get('inbox_max_pm'))*100) . '%',$pagination->total,$this->config->get('inbox_max_pm'));
                    $this->data['text_pm_reported']=$this->language->get('text_pm_reported');

                    if($messageTotal-$this->config->get('inbox_max_pm')<0){
                        $offset=0;
                    }else{
                        $offset=$messageTotal-$this->config->get('inbox_max_pm');
                    }

                    if($pagination->total==$this->config->get('inbox_max_pm')){
                        $this->data['is_folder_full']=TRUE;
                        $this->data['error_list_folder_full']= ($offset==0) ? $this->language->get('error_folder_full') : sprintf($this->language->get('error_list_folder_full'),$offset);
                    }else{
                        $this->data['is_folder_full']=FALSE;
                    }

                    //move offset to hide new pms
                    $start=($page-1)*$pagination->limit+$offset;

                    //get arrays of pms in inbox
                    $results=$this->model_community_ucp->loadPrivateMessages($start,$pagination->limit);

                    $this->data['inbox_pms']=array();

                    $i=0;
                    foreach($results as $result){
                        if($result['pm_unread']){
                            $readStatus=' message-unread';
                        }else{
                            $readStatus=$result['pm_replied'] ? ' message-replied' : ' message-read';
                        }

                        $this->data['inbox_pms'][]=array(
                            'msg_id'             =>  $result['msg_id'],
                            'msg_link'           =>  $this->url->link('community/ucp','mode=pm&action=ib&mid=' . $result['msg_id'],'SSL'),
                            'msg_history_id'     =>  $result['msg_history_id'],
                            'user_profile_url'   =>  $this->url->link('community/profile','uid=' . $result['author_id'] ,'SSL'),
                            'username'           =>  $result['username'],
                            'message_read_status'=>  $readStatus,
                            'message_subject'    =>  $result['message_subject'],
                            'message_time'       =>  date($this->language->get('date_time_format_long'),$result['message_time'])
                        );

                        if(isset($result['reporter_id']) && $result['reporter_id']==$this->customer->getForumUserId() && $result['message_reported']){
                            $this->data['inbox_pms'][$i]['is_reported']=TRUE;
                        }else{
                            $this->data['inbox_pms'][$i]['is_reported']=FALSE;
                        }

                        $i++;
                    }
                }

                break;
            case 'sm':
                if(isset($this->request->post['delete_sm']) && isset($this->request->post['marked_msg_id'])){
                    $idStr=implode(', ',$this->request->post['marked_msg_id']);

                    $this->model_community_ucp->deleteSentMessages($idStr,$this->request->post['marked_msg_id']);

                    $this->session->data['forwarded_link']=$this->url->link('community/ucp','mode=pm&action=sm','SSL');
                    $this->session->data['success_type']='pm_del';

                    $this->redirect($this->url->link('community/success','','SSL'));
                }

                $this->data['ucp_heading']=$this->language->get('text_sent_messages');
                $this->data['content_type']='pm_sm';
                $this->data['text_action']='sm';

                $this->data['text_to']=$this->language->get('text_to');
                $this->data['text_sent']=$this->language->get('text_sent');
                $this->data['text_no_messages']=$this->language->get('text_no_messages');
                $this->data['text_message']=$this->language->get('text_message');
                $this->data['text_mark']=$this->language->get('text_mark');
                $this->data['text_message_history']=$this->language->get('text_message_history');
                //button
                $this->data['text_delete_pm']=$this->language->get('text_delete_pm');
                $this->data['text_report_pm']=$this->language->get('text_report_pm');


                //sent message in detail view
                if(isset($this->request->get['mid'])){
                    $this->data['has_mid']='';

                    if(isset($this->request->post['delete_pm'])){
                        $idArr[]=$this->request->get['mid'];

                        $this->model_community_ucp->deleteSentMessages($this->request->get['mid'],$idArr);

                        $this->session->data['forwarded_link']=$this->url->link('community/ucp','mode=pm&action=sm','SSL');
                        $this->session->data['success_type']='pm_del';

                        $this->redirect($this->url->link('community/success','','SSL'));
                    }

                    //detect if folder is full
                    $messageTotal=$this->model_community_ucp->getSentMessageTotal();

                    if($messageTotal>$this->config->get('sent_message_max_pm')){
                        $validMessageTotal=$this->config->get('sent_message_max_pm');
                    }else{
                        $validMessageTotal=$messageTotal;
                    }

                    $sentMessageTotal=$validMessageTotal;

                    $this->data['text_sent_messages_folder_full']=sprintf($this->language->get('text_sent_messages_folder_full'),(($sentMessageTotal/$this->config->get('sent_message_max_pm'))*100) . '%',$sentMessageTotal,$this->config->get('sent_message_max_pm'));

                    if($sentMessageTotal==$this->config->get('sent_message_max_pm')){
                        $this->data['is_folder_full']=TRUE;
                        $this->data['error_folder_full']=$this->language->get('error_folder_full');
                    }else{
                        $this->data['is_folder_full']=FALSE;
                    }

                    $result=$this->model_community_ucp->viewSentMessage($this->request->get);

                    foreach(unserialize($result['to_address']) as $user_name=>$user_id){
                        $result['user_profile_urls'][]=$this->url->link('community/profile','uid=' . $user_id ,'SSL');
                        $result['usernames'][]=$user_name;
                    }

                    $this->data['sent_message']=array(
                        'author_id'         =>  $result['author_id'],
                        'user_profile_urls' =>  $result['user_profile_urls'],
                        'usernames'         =>  $result['usernames'],
                        'msg_history_id'    =>  $result['msg_history_id'],
                        'message_time'      =>  date($this->language->get('date_time_format_long'),$result['message_time']),
                        'message_subject'   =>  $result['message_subject'],
                        'message_text'      =>  $this->parseSmiles($this->parseBbcode($this->parseSpacing($result['message_text']))),
                        'delete_link'       =>  $this->url->link('community/',$result['msg_id'],'SSL'),
                        'report_link'       =>  $this->url->link('community/report','type=sm' . '&mid=' . $result['msg_id'],'SSL')
                    );

                    $this->data['action_link']=$this->url->link('community/ucp','mode=pm&action=sm&mid=' . $result['msg_id'],'SSL');

                    if(!$result['message_reported']){
                        $this->data['show_report_icon']=TRUE;
                    }else{
                        $this->data['show_report_icon']=FALSE;
                    }

                    //sent message history
                    $results=$this->model_community_ucp->viewSentMessageHistory($this->request->get,$result['msg_history_id']);

                    if(count($results)>1){
                        $this->data['has_message_history']=TRUE;
                    }else{
                        $this->data['has_message_history']=FALSE;
                    }

                    if($this->data['has_message_history']){
                        foreach($results as $result){
                            $this->data['sent_message_histories'][]=array(
                                'user_profile_url'  =>  $this->url->link('community/profile','uid=' . $result['author_id'] ,'SSL'),
                                'username'          =>  $result['username'],
                                'message_time'      =>  date($this->language->get('date_time_format_long'),$result['message_time']),
                                'message_subject'   =>  $result['message_subject'],
                                'message_text'      =>  $this->parseSmiles($this->parseBbcode($this->parseSpacing($result['message_text'])))
                            );
                        }
                    }
                }else{
                    $this->data['action_link']=$this->url->link('community/ucp','mode=pm&action=sm','SSL');
                    $this->data['newpm_link']=$this->url->link('community/ucp','mode=pm&action=cm','SSL');

                    //pagination
                    $pagination = new Pagination();

                    if (isset($this->request->get['page'])) {
                        $page = $this->request->get['page'];
                    } else {
                        $page = 1;
                    }

                    $pagination->page = $page;
                    $pagination->limit = 20;
                    $pagination->url = $this->url->link('community/ucp', '&mode=pm&action=sm&page={page}','SSL');

                    $this->data['pagination'] = $pagination->render();

                    //detect if folder is full
                    $messageTotal=$this->model_community_ucp->getSentMessageTotal();

                    if($messageTotal>$this->config->get('sent_message_max_pm')){
                        $validMessageTotal=$this->config->get('sent_message_max_pm');
                    }else{
                        $validMessageTotal=$messageTotal;
                    }

                    $this->data['sent_message_total']=$pagination->total=$validMessageTotal;

                    $this->data['text_sent_messages_folder_full']=sprintf($this->language->get('text_sent_messages_folder_full'),(($pagination->total/$this->config->get('sent_message_max_pm'))*100) . '%',$pagination->total,$this->config->get('sent_message_max_pm'));

                    if($messageTotal-$this->config->get('sent_message_max_pm')<0){
                        $offset=0;
                    }else{
                        $offset=$messageTotal-$this->config->get('sent_message_max_pm');
                    }

                    if($pagination->total==$this->config->get('sent_message_max_pm')){
                        $this->data['is_folder_full']=TRUE;
                        $this->data['error_list_folder_full']= ($offset==0) ? $this->language->get('error_folder_full') : sprintf($this->language->get('error_list_folder_full'),$offset);
                    }else{
                        $this->data['is_folder_full']=FALSE;
                    }

                    $this->data['text_pm_reported']=$this->language->get('text_pm_reported');

                    //move offset to hide new pms
                    $start=($page-1)*$pagination->limit+$offset;

                    //get arrays of sent messages
                    $results=$this->model_community_ucp->loadSentMessages($start,$pagination->limit);

                    $this->data['sent_messages']=array();

                    $i=0;
                    foreach($results as $result){
                        foreach(unserialize($result['to_address']) as $user_name=>$user_id){
                            $result['user_profile_urls'][]=$this->url->link('community/profile','uid=' . $user_id ,'SSL');
                            $result['usernames'][]=$user_name;
                        }

                        $this->data['sent_messages'][]=array(
                            'msg_id'             =>  $result['msg_id'],
                            'msg_link'           =>  $this->url->link('community/ucp','mode=pm&action=sm&mid=' . $result['msg_id'],'SSL'),
                            'msg_history_id'     =>  $result['msg_history_id'],
                            'author_id'          =>  $result['author_id'],
                            'usernames'          =>  $result['usernames'],
                            'user_profile_urls'  =>  $result['user_profile_urls'],
                            'pm_unread'          =>  $result['pm_unread'],
                            'pm_replied'         =>  $result['pm_replied'],
                            'message_subject'    =>  $result['message_subject'],
                            'message_time'       =>  date($this->language->get('date_time_format_long'),$result['message_time'])
                        );

                        if(isset($result['reporter_id']) && $result['reporter_id']==$this->customer->getForumUserId() && $result['message_reported']){
                            $this->data['sent_messages'][$i]['is_reported']=TRUE;
                        }else{
                            $this->data['sent_messages'][$i]['is_reported']=FALSE;
                        }

                        $i++;
                    }
                }
                break;
        }
    }

    private function friendsAndFoes(){
        $this->data['navi_ff_active']='class="navi_active"';

        $this->data['buttons']['mfr']=$this->language->get('button_manage_friends');
        $this->data['button_links'][]=$this->url->link('community/ucp','mode=ff&action=mfr','SSL');

        $this->data['buttons']['mfe']=$this->language->get('button_manage_foes');
        $this->data['button_links'][]=$this->url->link('community/ucp','mode=ff&action=mfe','SSL');

        $this->data['buttons']['afr']=$this->language->get('button_add_friends');
        $this->data['button_links'][]=$this->url->link('community/ucp','mode=ff&action=afr','SSL');

        $this->data['button_submit']=$this->language->get('button_submit');

        if(!isset($this->request->get['action'])){
            $this->request->get['action']='mfr';
        }

        switch($this->request->get['action']){
            case 'mfr':
                $this->data['ucp_heading']=$this->language->get('text_manage_friends');
                $this->data['content_type']='ff_mfr';
                $this->data['text_action']='mfr';

                if(isset($this->request->get['frid'])){
                    $this->data['has_frid']='';
                }else{
                    $this->data['text_manage_friends_intro']=$this->language->get('text_manage_friends_intro');
                    $this->data['text_quick_access']=$this->language->get('text_quick_access');
                }

                break;
            case 'mfe':
                $this->data['ucp_heading']=$this->language->get('text_manage_foes');
                $this->data['content_type']='ff_mfe';
                $this->data['text_action']='mfe';
                break;
            case 'afr':
                $this->data['ucp_heading']=$this->language->get('text_add_friends');
                $this->data['content_type']='ff_afr';
                $this->data['text_action']='afr';
                $this->data['text_add_friends_intro']=$this->language->get('text_add_friends_intro');

                $this->data['action_link']=$this->url->link('community/ucp','mode=ff&action=afr','SSL');

                if(isset($this->request->post['submit']) && $this->friendRequestValid()){

                }
                break;
        }
    }

    private function profileEditValidate(){
        if(utf8_strlen($this->request->post['icq']) > 15){
            $this->error['icq']=sprintf($this->language->get('error_length'),15);
        }

        if(utf8_strlen($this->request->post['aol']) > 255){
            $this->error['aol']=sprintf($this->language->get('error_length'),255);
        }

        if(utf8_strlen($this->request->post['wl_msn']) > 255){
            $this->error['wl_msn']=sprintf($this->language->get('error_length'),255);
        }

        if(utf8_strlen($this->request->post['yahoo']) > 255){
            $this->error['yahoo']=sprintf($this->language->get('error_length'),255);
        }

        if(utf8_strlen($this->request->post['jabber']) > 255){
            $this->error['jabber']=sprintf($this->language->get('error_length'),255);
        }

        if(!(($this->request->post['birthday_day'] =='--'  && $this->request->post['birthday_month'] =='--' && $this->request->post['birthday_year'] =='--') || ($this->request->post['birthday_day'] !='--'  && $this->request->post['birthday_month'] !='--' && $this->request->post['birthday_year'] !='--'))){
            $this->error['birthday']=$this->language->get('error_birthday');
        }

        if(!$this->error){
            return true;
        }else{
            return false;
        }
    }

    private function avatarValidate(){
        //avatar upload part1 begin
        if($this->request->files['avatar']['error']>0){
            $this->config->get('max_avatar_size');//to-do: add avatar max size in admin:$_config['max_avatar_size'], unit:MB
            switch($this->request->files['avatar']['error']){
                case 2:
                    $this->error['avatar'] = sprintf($this->language->get('error_max_avatar_size'), $this->config->get('max_avatar_size'));
                    break;
                case 3:
                    $this->error['avatar'] = $this->language->get('error_uploadstop');
                    break;
            }
        }

        $temp=explode('.',$this->request->files['avatar']['name']);
        $exName=$temp[count($temp)-1];
        //to-do: add avatar file types in admin $_config['allowtypes']. Note value entered in admin must be strtollowered!
        if($exName && !in_array($exName,$this->config->get('allowtypes'))){
            $allowTypes=implode(',',$this->config->get('allowtypes'));
            $this->error['avatar'] = sprintf($this->language->get('error_avatartypes'),$allowTypes);
        }

        if(is_uploaded_file($this->request->files['avatar']['tmp_name'])){
            $newName = mt_rand(0,9999) . time() . '_' . $this->request->files['avatar']['name'];
            $newPath = 'catalog/view/theme/default/image/avatar/' . $newName;
        }
        //avatar upload part1 end

        if (!$this->error) {
            //avatar upload part2 begin
            if(!move_uploaded_file($this->request->files['avatar']['tmp_name'],$newPath)){
                $this->error['avatar'] = $this->language->get('error_avatarfail');
            }else{
                //resample avatar
                list($width_orig,$height_orig)=getimagesize($newPath);
                $max_width = (int)$this->config->get('max_avatar_width');
                $max_height = (int)$this->config->get('max_avatar_height');
                //to-do:add avatar image max width and max height in admin $_config
                if($width_orig>$max_width || $height_orig>$max_height){
                    if($max_width && $width_orig<$height_orig){
                        $max_width = ($max_height/$height_orig) * $width_orig;
                    }else{
                        $max_height = ($max_width/$width_orig) * $height_orig;
                    }

                    switch($exName){
                        case 'gif':
                            $srcImg = imagecreatefromgif($newPath);
                            break;
                        case "jpg":
                            $srcImg = imagecreatefromjpeg($newPath);
                            break;
                        case 'png':
                            $srcImg = imagecreatefrompng($newPath);
                            break;
                        case 'wbmp':
                            $srcImg = imagecreatefromwbmp($newPath);
                            break;
                    }
                    $srcImg = imagecreatefromjpeg($newPath);
                    $dstImg=imagecreatetruecolor($max_width,$max_height);
                    imagecopyresampled($dstImg,$srcImg,0,0,0,0,$max_width,$max_height,$width_orig,$height_orig);

                    switch($exName){
                        case 'gif':
                            imagegif($dstImg,$newPath);
                            break;
                        case 'jpg':
                            imagejpeg($dstImg,$newPath);
                            break;
                        case 'png':
                            imagepng($dstImg,$newPath);
                            break;
                        case 'wbmp':
                            imagewbmp($dstImg,$newPath);
                            break;
                    }
                    imagedestroy($srcImg);
                    imagedestroy($dstImg);
                }
            }
            return $newPath;
            //avatar upload part2 end
        }else{
            return FALSE;
        }
    }

    private function signatureValidate(){
        if(utf8_strlen($this->request->post['content']) > 255){
            $this->error['content']=sprintf($this->language->get('error_content'),255);
        }

        if(!$this->error){
            return true;
        }else{
            return false;
        }
    }

    private function forumUsernameValidate(){
        $this->load->model('community/user');

        if ((utf8_strlen($this->request->post['forumusername']) < 1) || (utf8_strlen($this->request->post['forumusername']) > 32)) {
            $this->error['forumusername'] = $this->language->get('error_forumusername');
        }

        if($this->model_community_user->getTotalUsersByForumUserName(utf8_strtolower($this->request->post['forumusername']))){
            $this->error['forumusername'] = $this->language->get('error_forumusernameexist');
        }

        if(in_array(utf8_strtolower($this->request->post['forumusername']),$this->config->get('disallow_username'))){
            $this->error['forumusername'] = $this->language->get('error_forumusernamedisallow');
        }

        if(!$this->error){
            return TRUE;
        }else{
            return FALSE;
        }
    }

    private function pmValidate(){
        if(utf8_strlen($this->request->post['subject']) > floor($this->config->get('posting_max_subject_length')/2)){
            $this->error['subject']=sprintf($this->language->get('error_subject'),floor($this->config->get('posting_max_subject_length')/2));
        }

        if($this->request->post['subject']==''){
            $this->error['subject']=$this->language->get('error_empty_subject');
        }

        if(utf8_strlen($this->request->post['content']) > floor($this->config->get('posting_max_content_length')/2)){
            $this->error['content']=sprintf($this->language->get('error_content'),floor($this->config->get('posting_max_content_length')/2));
        }

        if($this->request->post['content']==''){
            $this->error['content']=$this->language->get('error_empty_content');
        }

        if(!$this->error){
            return true;
        }else{
            return false;
        }
    }

    private function friendRequestValid(){
        if($this->request->post['friend-names']==''){
            $this->error['friend_names']=$this->language->get('error_empty_friend_names');
        }

        if($this->model_community_ucp->checkFriendUserNamesExist($this->request->post['friend-names'])){
            $this->error['friend_names']=$this->language->get('error_friend_names_not_exist');
        }
    }

    private function generateBbcode(){
        $bbButtons=array();
        $bbtags=array();

        $bbButtons[]='<select id="font-size" name="font-size" title="' . $this->language->get('text_font_size') . '" onchange="bbfontstyle(\'[size=\' + this.options[this.selectedIndex].value + \']\', \'[/size]\');this.selectedIndex = 2;">
                            <option value="50">' . $this->language->get('text_tiny') . '</option>
                            <option value="85">' . $this->language->get('text_small') . '</option>
                            <option selected="selected" value="100">' . $this->language->get('text_normal') . '</option>
                            <option value="150">' . $this->language->get('text_large') . '</option>
                            <option value="200">' . $this->language->get('text_huge') . '</option>
                          </select>';
        $bbtags[]="'[size=]'";
        $bbtags[]="'[/size]'";

        $bbButtons[]='<input type="button" id="bold_text" title="' . $this->language->get('text_bold_text') . '" onclick="bbstyle(2)">';
        $bbtags[]="'[b]'";
        $bbtags[]="'[/b]'";

        $bbButtons[]='<input type="button" id="italic_text" title="' . $this->language->get('text_italic_text') . '" onclick="bbstyle(4)">';
        $bbtags[]="'[i]'";
        $bbtags[]="'[/i]'";

        $bbButtons[]='<input type="button" id="underline_text" title="' . $this->language->get('text_underline_text') . '" onclick="bbstyle(6)">';
        $bbtags[]="'[u]'";
        $bbtags[]="'[/u]'";

        //no bbtags for font color button, has been added in the editor.js file
        $bbButtons[]='<input type="button" id="font_color" title="' . $this->language->get('text_font_color') . '" onclick="change_palette(\'font_colour_palette\');">';

        $bbButtons[]='<input type="button" id="quote_text" title="' . $this->language->get('text_quote_text') . '" onclick="bbstyle(8)">';
        $bbtags[]="'[quote]'";
        $bbtags[]="'[/quote]'";

        $bbButtons[]='<input type="button" id="code_display" title="' . $this->language->get('text_code_display') . '" onclick="bbstyle(10)">';
        $bbtags[]="'[code]'";
        $bbtags[]="'[/code]'";

        if($this->config->get('bbcode_url_parsing_enable')){
            $bbButtons[]='<input type="button" id="insert_URL" title="' . $this->language->get('text_insert_URL') . '" onclick="bbstyle(12)">';

            $this->data['bbcode_url_enable']=TRUE;
        }else{
            $this->data['bbcode_url_enable']=FALSE;
        }
        $bbtags[]="'[url]'";
        $bbtags[]="'[/url]'";

        if($this->config->get('bbcode_img_parsing_enable')){
            $bbButtons[]='<input type="button" id="insert_image" title="' . $this->language->get('text_insert_image') . '" onclick="bbstyle(14)">';
            $this->data['bbcode_img_enable']=TRUE;
        }else{
            $this->data['bbcode_img_enable']=FALSE;
        }
        $bbtags[]="'[img]'";
        $bbtags[]="'[/img]'";

        if($this->config->get('bbcode_flash_parsing_enable')){
            $bbButtons[]='<input type="button" id="insert_flash" title="' . $this->language->get('text_insert_flash') . '" onclick="bbstyle(16)">';
            $this->data['bbcode_flash_enable']=TRUE;
        }else{
            $this->data['bbcode_flash_enable']=FALSE;
        }
        $bbtags[]="'[flash]'";
        $bbtags[]="'[/flash]'";

        $bbButtons[]='<input type="button" id="insert_youtube" title="' . $this->language->get('text_insert_youtube') . '" onclick="bbstyle(18)">';
        $bbtags[]="'[youtube]'";
        $bbtags[]="'[/youtube]'";

        return array($bbButtons,$bbtags);
    }

    private function generateSmiles(){
        if($this->config->get('smiles_parsing_enable')){
            $smileStr='';

            $smileStr.='<img onclick="insert_text(\':D\', true); return false;" src="catalog/view/theme/default/image/smilies/01_very_happy.gif" alt=":D" title="' . $this->language->get('text_very_happy') . '" />';
            $smileStr.='<img onclick="insert_text(\':)\', true); return false;" src="catalog/view/theme/default/image/smilies/02_smile.gif" alt=":)" title="' . $this->language->get('text_smile') . '" />';
            $smileStr.='<img onclick="insert_text(\';)\', true); return false;" src="catalog/view/theme/default/image/smilies/03_wink.gif" alt=";)" title="' . $this->language->get('text_wink') . '" />';
            $smileStr.='<img onclick="insert_text(\':(\', true); return false;" src="catalog/view/theme/default/image/smilies/04_sad.gif" alt=":(" title="' . $this->language->get('text_sad') . '" />';
            $smileStr.='<img onclick="insert_text(\':o\', true); return false;" src="catalog/view/theme/default/image/smilies/05_surprised.gif" alt=":o" title="' . $this->language->get('text_surprised') . '" />';
            $smileStr.='<img onclick="insert_text(\':shock:\', true); return false;" src="catalog/view/theme/default/image/smilies/06_shocked.gif" alt=":shock:" title="' . $this->language->get('text_shocked') . '" />';
            $smileStr.='<img onclick="insert_text(\':?\', true); return false;" src="catalog/view/theme/default/image/smilies/07_confused.gif" alt=":?" title="' . $this->language->get('text_confused') . '" />';
            $smileStr.='<img onclick="insert_text(\'8-)\', true); return false;" src="catalog/view/theme/default/image/smilies/08_cool.gif" alt="8-)" title="' . $this->language->get('text_cool') . '" />';
            $smileStr.='<img onclick="insert_text(\':lol:\', true); return false;" src="catalog/view/theme/default/image/smilies/09_langhing.gif" alt=":lol:" title="' . $this->language->get('text_langhing') . '" />';
            $smileStr.='<img onclick="insert_text(\':x\', true); return false;" src="catalog/view/theme/default/image/smilies/10_mad.gif" alt=":x" title="' . $this->language->get('text_mad') . '" />';
            $smileStr.='<img onclick="insert_text(\':P\', true); return false;" src="catalog/view/theme/default/image/smilies/11_razz.gif" alt=":P" title="' . $this->language->get('text_razz') . '" />';
            $smileStr.='<img onclick="insert_text(\':-oops:\', true); return false;" src="catalog/view/theme/default/image/smilies/12_embarrassed.gif" alt=":-oops:" title="' . $this->language->get('text_embarrassed') . '" />';
            $smileStr.='<img onclick="insert_text(\':cry:\', true); return false;" src="catalog/view/theme/default/image/smilies/13_cry.gif" alt=":cry:" title="' . $this->language->get('text_cry') . '" />';
            $smileStr.='<img onclick="insert_text(\':evil:\', true); return false;" src="catalog/view/theme/default/image/smilies/14_evil_or_very_mad.gif" alt=":evil:" title="' . $this->language->get('text_evil_or_very_mad') . '" />';
            $smileStr.='<img onclick="insert_text(\':twisted:\', true); return false;" src="catalog/view/theme/default/image/smilies/15_twisted_evil.gif" alt=":twisted:" title="' . $this->language->get('text_twisted_evil') . '" />';
            $smileStr.='<img onclick="insert_text(\':roll:\', true); return false;" src="catalog/view/theme/default/image/smilies/16_rolling_eyes.gif" alt=":roll:" title="' . $this->language->get('text_rolling_eyes') . '" />';
            $smileStr.='<img onclick="insert_text(\':!:\', true); return false;" src="catalog/view/theme/default/image/smilies/17_exclamation.gif" alt=":!:" title="' . $this->language->get('text_exclamation') . '" />';
            $smileStr.='<img onclick="insert_text(\':-?:\', true); return false;" src="catalog/view/theme/default/image/smilies/18_question.gif" alt=":-?:" title="' . $this->language->get('text_question') . '" />';
            $smileStr.='<img onclick="insert_text(\':idea:\', true); return false;" src="catalog/view/theme/default/image/smilies/19_idea.gif" alt=":idea:" title="' . $this->language->get('text_idea') . '" />';
            $smileStr.='<img onclick="insert_text(\':arrow:\', true); return false;" src="catalog/view/theme/default/image/smilies/20_arrow.gif" alt=":arrow:" title="' . $this->language->get('text_arrow') . '" />';
            $smileStr.='<img onclick="insert_text(\':|\', true); return false;" src="catalog/view/theme/default/image/smilies/21_neutral.gif" alt=":|" title="' . $this->language->get('text_neutral') . '" />';
            $smileStr.='<img onclick="insert_text(\':mrgreen:\', true); return false;" src="catalog/view/theme/default/image/smilies/22_mr._green.gif" alt=":mrgreen:" title="' . $this->language->get('text_mr_green') . '" />';
            $smileStr.='<img onclick="insert_text(\':geek:\', true); return false;" src="catalog/view/theme/default/image/smilies/23_geek.gif" alt=":geek:" title="' . $this->language->get('text_geek') . '" />';
            $smileStr.='<img onclick="insert_text(\':ugeek:\', true); return false;" src="catalog/view/theme/default/image/smilies/24_uber_geek.gif" alt=":ugeek:" title="' . $this->language->get('text_uber_geek') . '" />';

            $this->data['smiles_enable']=TRUE;

            return $smileStr;
        }else{
            $this->data['smiles_enable']=FALSE;
        }
    }

    private function parseSpacing($str){
        $str=nl2br($str);
        $str = str_replace("\t", '&nbsp; &nbsp;', $str);
        $str = str_replace('  ', '&nbsp; ', $str);
        $str = str_replace('  ', ' &nbsp;', $str);

        return $str;
    }

    private function parseBbcode($str){
        if($this->config->get('bbcode_parsing_enable')){
            $pattern=array();
            $pattern[]='/\[size=(\d+)\](.*)\[\/size\]/Uis';
            $pattern[]='/\[b\](.*)\[\/b\]/Uis';
            $pattern[]='/\[i\](.*)\[\/i\]/Uis';
            $pattern[]='/\[u\](.*)\[\/u\]/Uis';
            $pattern[]='/\[color=(#[0-9a-f]{3}|#[0-9a-f]{6}|[a-z\-]+)\](.*)\[\/color\]/Uis';

            if($this->config->get('bbcode_url_parsing_enable')){
                $pattern[]='/\[url\](.*)\[\/url\]/Uis';
                $pattern[]='/\[url=([^\[]+?)\](.*)\[\/url\]/Uis';
            }

            $pattern[]='/\[quote(?!&quot;.*&quot;)\](.*)\[\/quote\]/Uis';
            $pattern[]='/\[quote=&quot;(.*)&quot;\](.*)\[\/quote\]/Uis';
            $pattern[]='/\[code\](.*)\[\/code\]/Uis';

            if($this->config->get('bbcode_img_parsing_enable')){
                $pattern[]='/\[img\](.*)\[\/img\]/Uis';
            }

            if($this->config->get('bbcode_flash_parsing_enable')){
                $pattern[]='/\[flash\](.*)\[\/flash\]/Uis';
            }

            $pattern[]='/\[youtube\](http:\/\/)?(www\.)?youtube.com\/watch\?v=(.*)(&amp;\w*=.*)?\[\/youtube\]/Uis';

            $replace=array();
            $replace[]='<span style="font-size:\1%;">\2</span>';
            $replace[]='<strong>\1</strong>';
            $replace[]='<em>\1</em>';
            $replace[]='<span style="text-decoration:underline;">\1</span>';
            $replace[]='<span style="color:\1;">\2</span>';

            if($this->config->get('bbcode_url_parsing_enable')){
                $replace[]='<a href="\1" target="_blank">\1</a>';
                $replace[]='<a href="\1" target="_blank">\2</a>';
            }

            $replace[]='<blockquote class="bbcode_uncited"><div>\1</div></blockquote>';
            $replace[]='<blockquote class="bbcode_uncited"><div><dl><dt>\1 Wrote:</dt><dd>\2</dd></dl></div></blockquote>';
            $replace[]='<dl class="bbcode_code"><dt>CODE: <a href="javascript:void(0)" onclick="selectCode(this); return FALSE;">SELECT ALL</a></dt><dd><code>\1</code></dd></dl>';

            if($this->config->get('bbcode_img_parsing_enable')){
                $replace[]='<img src="\1" />';
            }

            if($this->config->get('bbcode_flash_parsing_enable')){
                $replace[]='<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" height="400" width="480"><param name="quality" value="high" /><param name="movie" value="\1" /><embed height="400" pluginspage="http://www.macromedia.com/go/getflashplayer" quality="high" src="\1" type="application/x-shockwave-flash" width="480"></embed></object>';
            }

            $replace[]='<iframe width="560" height="315" src="http://www.youtube.com/embed/\3" frameborder="0" allowfullscreen></iframe>';

            $str=preg_replace($pattern,$replace,$str);
        }

        return $str;
    }

    private function parseSmiles($str){
        if($this->config->get('smiles_parsing_enable')){
            $smileCode=array(
                ':D',
                ':)',
                ';)',
                ':(',
                ':o',
                ':shock:',
                ':?',
                '8-)',
                ':lol:',
                ':x',
                ':P',
                ':-oops:',
                ':cry:',
                ':evil:',
                ':twisted:',
                ':roll:',
                ':!:',
                ':-?:',
                ':idea:',
                ':arrow:',
                ':|',
                ':mrgreen:',
                ':geek:',
                ':ugeek:',
            );

            $smileImage=array(
                '<img src="catalog/view/theme/default/image/smilies/01_very_happy.gif" />',
                '<img src="catalog/view/theme/default/image/smilies/02_smile.gif" />',
                '<img src="catalog/view/theme/default/image/smilies/03_wink.gif" />',
                '<img src="catalog/view/theme/default/image/smilies/04_sad.gif" />',
                '<img src="catalog/view/theme/default/image/smilies/05_surprised.gif" />',
                '<img src="catalog/view/theme/default/image/smilies/06_shocked.gif" />',
                '<img src="catalog/view/theme/default/image/smilies/07_confused.gif" />',
                '<img src="catalog/view/theme/default/image/smilies/08_cool.gif" />',
                '<img src="catalog/view/theme/default/image/smilies/09_langhing.gif" />',
                '<img src="catalog/view/theme/default/image/smilies/10_mad.gif" />',
                '<img src="catalog/view/theme/default/image/smilies/11_razz.gif" />',
                '<img src="catalog/view/theme/default/image/smilies/12_embarrassed.gif" />',
                '<img src="catalog/view/theme/default/image/smilies/13_cry.gif" />',
                '<img src="catalog/view/theme/default/image/smilies/14_evil_or_very_mad.gif" />',
                '<img src="catalog/view/theme/default/image/smilies/15_twisted_evil.gif" />',
                '<img src="catalog/view/theme/default/image/smilies/16_rolling_eyes.gif" />',
                '<img src="catalog/view/theme/default/image/smilies/17_exclamation.gif" />',
                '<img src="catalog/view/theme/default/image/smilies/18_question.gif" />',
                '<img src="catalog/view/theme/default/image/smilies/19_idea.gif" />',
                '<img src="catalog/view/theme/default/image/smilies/20_arrow.gif" />',
                '<img src="catalog/view/theme/default/image/smilies/21_neutral.gif" />',
                '<img src="catalog/view/theme/default/image/smilies/22_mr._green.gif" />',
                '<img src="catalog/view/theme/default/image/smilies/23_geek.gif" />',
                '<img src="catalog/view/theme/default/image/smilies/24_uber_geek.gif" />'
            );

            $str=str_replace($smileCode,$smileImage,$str);
        }

        return $str;
    }

    private function parseURL($str){
        if($this->config->get('auto_url_parsing_enable')){
            $pattern=$replace=array();
            // matches an "xxxx://yyyy" URL at the start of a line, or after a space.xxxx can only be alpha characters. yyyy is anything up to the first space, newline, comma, double quote or <
            $pattern[]='/(^|[\n ])([\w]+?:\/\/[^ \"\n\r\t<]*)/is';
            // matches a "www|ftp.xxxx.yyyy[/zzzz]" kinda lazy URL thing.Must contain at least 2 dots. xxxx contains either alphanum, or "-". zzzz is optional.. will contain everything up to the first space, newline,comma, double quote or <.
            $pattern[]='/(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)/is';
            // matches an email@domain type address at the start of a line, or after a space. Note: Only the followed chars are valid; alphanums, "-", "_" and or ".".
            $pattern[]='/(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)/i';

            $replace[]='\\1<a href="\\2" target="_blank">\\2</a>';
            $replace[]='\\1<a href="http://\\2" target="_blank">\\2</a>';
            $replace[]='\\1<a href="mailto:\\2@\\3">\\2@\\3</a>';

            $str=preg_replace($pattern,$replace,$str);
        }

        return $str;
    }
}
?>